#ifndef TCPSCANNER_H
#define TCPSCANNER_H

#include <QString>
#include <string>
#include <windows.h>
#include "Delegate.h"

DECLARE_MULTICAST_DELEGATE(OnResultSignature, QVector<QString>);
DECLARE_MULTICAST_DELEGATE(OnEndSignature);

typedef struct ip_header
{
    char version : 4;
    char headerlength : 4;
    char cTOS;
    unsigned short totla_length;
    unsigned short identification;
    unsigned short flags_offset;
    char time_to_live;
    char Protocol;
    unsigned short check_sum;
    unsigned int SrcAddr;
    unsigned int DstAddr;
}IP_HEADER;

// 伪TCP报头
typedef struct psd_hdr
{
    unsigned long saddr;  //源地址
    unsigned long daddr; //目的地址
    char mbz; //强制置空
    char ptcl; //协议类型
    unsigned short tcpl; //TCP长度
}PSD_HEADER;

//定义TCP报头
typedef struct _tcphdr
{
    unsigned short th_sport; //16位源端口
    unsigned short th_dport; //16位目的端口
    unsigned int th_seq; //32位序列号
    unsigned int th_ack; //32位确认号
    unsigned char th_lenres; //4位首部长度/4位保留字
    unsigned char th_flag; //6位标志位
    unsigned short th_win; //16位窗口大小
    unsigned short th_sum; //16位校验和
    unsigned short th_urp; //16位紧急数据偏移量

} TCP_HEADER;

// 打开网卡
void OpenAdapter(std::string local_address);

// 构造UDP报文
void CreateUDPPacket(unsigned char* SourceMAC, unsigned char* DestinationMAC,unsigned long SourceIP, unsigned long DestIP,unsigned short SourcePort, unsigned short DestinationPort,unsigned char* UserData, unsigned int UserDataLength);

// 构造TCP报文
void CreateTCPPacket(unsigned char* SourceMAC, unsigned char* DestinationMAC,unsigned long SourceIP, unsigned long DestIP, unsigned short SourcePort,
                     unsigned short DestinationPort, int flag = 2);



class TCPScanner
{
public:
    TCPScanner();

    // TCP Connect
    void TCP_Connect_Scan(const char* StartIP, const char* DestIP, unsigned short StartPort, unsigned short EndPort);
    void TCP_Connect_Scan(const char* DestIP, unsigned short StartPort, unsigned short EndPort);

    OnResultSignature OnResult;
    OnResultSignature OnSynResult;
    OnResultSignature OnFinResult;

    OnEndSignature OnEnd;

    void TCP_Syn_Scan(const char* DestIP, unsigned short StartPort, unsigned short EndPort);

    void SetSynStopping(bool bIsStop);

    void TCP_Fin_Scan(const char* DestIP, unsigned short StartPort, unsigned short EndPort);

    void SetFinStopping(bool bIsStop);

    void UDP_Scan(const char* DestIP, unsigned short StartPort, unsigned short EndPort);

private:
    void Init();

    bool bIsStopping = false;

    bool bSynIsStopping = false;
};

#endif // TCPSCANNER_H
